package com.tsu.mall.admin.controller;

import com.tsu.mall.common.VO.DetailInfoVo;
import com.tsu.mall.common.VO.LayuiTableVO;
import com.tsu.mall.common.VO.ResultVO;
import com.tsu.mall.common.entity.Bill;
import com.tsu.mall.common.service.IBillDetailService;
import com.tsu.mall.common.service.IBillService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

//订单表
@Controller
@RequestMapping("/bill")
public class BillController {
    @Autowired
    private IBillService iBillService;
    @Autowired
    private IBillDetailService iBillDetailService;

    @GetMapping("/billPage")
    public String billPage(){
        return "bill";
    }
    @PostMapping("/billDetailInfo")
    @ResponseBody
    public List<DetailInfoVo> selectBillDetail(Integer billId){
        //查询详情页面需要展示的数据
        return iBillDetailService.selectBillDetail(billId);
    }

    @GetMapping("/list")
    @ResponseBody
    public ResultVO billList(Integer page,Integer limit,String accountNumber,String code,String billStatus){
        return iBillService.list(page,limit,accountNumber,code,billStatus);
    }
    @PostMapping("/updateStatus")
    @ResponseBody
    public LayuiTableVO updateStatus(Integer id){
        return iBillService.doUpdateStatus(id);
    }
    @PostMapping("/export")
    @ResponseBody
    public void export(String accountNumber, String code, String billStatus, HttpServletResponse response){
        List<Bill> list=iBillService.doList(accountNumber,code,billStatus);
        try{
            //从项目类的加载路径中找到模块
            ClassPathResource cpr=new ClassPathResource("/templates/export/bill.xlsx");
            //用流读入模版
            InputStream in=cpr.getInputStream();
            Workbook workbook = new XSSFWorkbook(in);
            Sheet sheet=workbook.getSheetAt(0);
            for(int i=1;i<=list.size();i++){
                Row row=sheet.createRow(i);
                Cell indexCell=row.createCell(0);
                Cell codeCell=row.createCell(1);
                Cell priceCell=row.createCell(2);
                Cell timeCell=row.createCell(3);
                Cell statusCell=row.createCell(4);
                Cell payTypeCell=row.createCell(5);
                Cell serialNumberCell=row.createCell(6);
                Cell remarkCell=row.createCell(7);

                indexCell.setCellValue(i);
                codeCell.setCellValue(list.get(i-1).getCode());
                priceCell.setCellValue(list.get(i-1).getPrice().toString());


                // LocalDateTime to Date
                LocalDateTime localDateTime = list.get(i-1).getCreateTime();
                ZoneId zoneId = ZoneId.systemDefault(); // 获取系统时间所在时区
                Date date = Date.from(localDateTime.atZone(zoneId).toInstant()); // 转换为 Date

                // 设置日期格式并写入单元格
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                timeCell.setCellValue(dateFormat.format(date));


                statusCell.setCellValue(list.get(i-1).getStatus());
                payTypeCell.setCellValue(list.get(i-1).getPayType());
                serialNumberCell.setCellValue(list.get(i-1).getSerialNumber());
                remarkCell.setCellValue(list.get(i-1).getRemark());

                //通过response写文件
                response.setCharacterEncoding("UTF-8");
                response.setHeader("Content-Type","application/vnd.ms-excel");
                response.setHeader("Content-Disposition","attachment;filename=\""+System.currentTimeMillis()+".xlsx\"");
                workbook.write(response.getOutputStream());
            }
        }catch (Exception e) {
            e.printStackTrace();
            System.out.println("导出出现异常");
        }
    }
}
